package com.tanjun.data.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

@Slf4j
@Configuration
public class RedissonConfig {
    @Value("${spring.data.redis.host:106.54.21.31}")
    private String host;
    @Value("${spring.data.redis.port:6379}")
    private Integer port;
    @Value("${spring.data.redis.password:}")
    private String password;
    @Value("${spring.data.redis.database:1}")
    private Integer db;

    @Bean
    public RedissonClient redissonClient() {
        log.info("redis host:{},port:{}",host,port);
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port)
                .setPassword(StringUtils.hasText(password)?password:null)
                .setDatabase(db)
                .setConnectionMinimumIdleSize(3)
                .setConnectionPoolSize(5);
        return Redisson.create(config);
    }
}
